package leetcode21_30;

import outoforder.leetcode.ListNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author shhjiang_001
 * @create 2020-06-13 22:47
 */
public class Solution23 {
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode l =  new ListNode();
        ListNode empt = l;
        // StringBuilder sb = new StringBuilder();
        List<Integer> mergeList = new ArrayList();
        for(int i = 0;i< lists.length;i++){
            ListNode LN = lists[i];
            if(lists[i] != null){
                // method1(LN,sb);
                method1(LN,mergeList);
            }
        }
        // char[] arr = sb.toString().toCharArray();
        int size = mergeList.size();
        Integer[] arr = mergeList.toArray(new Integer[size]);
        Arrays.sort(arr);
        for(int j = 0;j < arr.length;j++){
            int num = arr[j]-'0';
            empt.next = new ListNode();
            empt = empt.next;
            empt.val = num;
        }
        return l.next;

    }
    // public void method1(ListNode LN, StringBuilder sb){
    public void method1(ListNode LN, List<Integer> mergeList){
        if(LN == null){
            return;
        }
        // sb.append(LN.val);
        mergeList.add(LN.val);
        LN = LN.next;
        // method1(LN,sb);
        method1(LN,mergeList);
    }
}
